<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.UUID" %>
<%
    JSONObject jsonData = new JSONObject();
    String requestid = Util.null2String( request.getParameter( "requestid" ) );
    String tablename = Util.null2String( request.getParameter( "tablename" ) );
    String workflowid = Util.null2String( request.getParameter( "workflowid" ) );
    User user = HrmUserVarify.getUser( request, response );
    int userId = user.getUID();
    boolean flag = false;
    try {
        String sql = "select id from " + tablename + " where requestid=?";
        rs.executeQuery( sql, requestid );
        rs.next();
        String mainid = Util.null2String( rs.getString( "id" ) );//mainid
        String dtl = getTableDtl( workflowid );
        bb.writeLog( "mainid=" + mainid + ",dtl=" + dtl );
        rs.executeQuery( "select skrmcskdwmc,khyxjqdzx,zh from " + tablename + "_" + dtl + " where mainid='" + mainid + "'" );
        while (rs.next()) {
            String skrmcskdwmc = Util.null2String( rs.getString( "skrmcskdwmc" ) );//收款单位
            String khyxjqdzx = Util.null2String( rs.getString( "khyxjqdzx" ) );//开户银行
            String zh = Util.null2String( rs.getString( "zh" ) );//账号
            String modeid = hasExist( skrmcskdwmc );
            bb.writeLog( "modeid=" + modeid );
            if (!"".equals( modeid )) {//存在则更新
                Object[] params = new Object[]{skrmcskdwmc, khyxjqdzx, zh, modeid};
                flag = rs.executeUpdate( "update uf_skrskdw set skrmcskdwmc=?,khyx=?,zh=? where id=?", params );
            } else {//不存在则插入
                Map<String, String> map = new HashMap<>();
                map.put( "skrmcskdwmc", skrmcskdwmc );
                map.put( "khyx", khyxjqdzx );
                map.put( "zh", zh );
                insertModelData( 19, userId, map );
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    jsonData.put( "flag", flag );
    out.print( jsonData.toJSONString() );
    bb.writeLog( "getInvoice_Ajax=" + jsonData.toJSONString() );
%>
<%!
    //判断是否在建模表中已经存在
    public static String hasExist(String name) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from uf_skrskdw where skrmcskdwmc='" + name + "'" );
        rs.next();
        String id = Util.null2String( rs.getString( "id" ) );//mainid
        return id;
    }

    //获取明细表
    public static String getTableDtl(String workflowid) {
        String dtl = "";
        switch (workflowid) {
            case "306":
                dtl = "dt3";//各类费用报销
                break;
            case "305":
                dtl = "dt6";//差路费报销
                break;
            default:
                dtl = "dt2";
        }
        return dtl;
    }

    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery( sql, modeid );
        rs.next();
        String tablename = Util.null2String( rs.getString( "tablename" ) );
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate( insSql, params )) {
            rs1.executeQuery( "select id from " + tablename + " where uuid=?", uuid );
            rs1.next();
            dataid = Util.getIntValue( rs1.getString( "id" ) );
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get( key ).toString() + "',";
                }
                if (updatesql.endsWith( "," )) {
                    updatesql = updatesql.substring( 0, updatesql.length() - 1 );
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate( updatesql );
                }
                //进行权限重构
                getNewModeRight( userid, modeid, dataid );
            }
        }
        return dataid;
    }

    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id );
    }
%>